package com.huanf.controller;

import com.huanf.domain.MySQLToCsvVo;
import com.huanf.service.MySQLToCsvService;
import com.huanf.utils.MyCsvFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 35238
 * 功能:
 * 时间: 2023-12-24 23:06
 */
@RestController
public class MySQLToCsvController {

    @Autowired
    private MySQLToCsvService MySQLToCSVService;

    @GetMapping("/toCsv") //浏览器访问 http://localhost:8080/toCsv
    public void demo() {

        //从数据库查询数据
        List<MySQLToCsvVo> districts = MySQLToCSVService.list();

        //文件保存到哪里
        String fileName = "E:\\桌面\\Java-Ready\\conf\\Excel\\" + MyCsvFileUtil.buildCsvFileName(districts);

        //创建表格行标题
        String tableNames = MyCsvFileUtil.buildCsvFileTableNames(districts);

        //创建文件
        MyCsvFileUtil.writeFile(fileName, tableNames);

        //写入数据
        String contentBody = MyCsvFileUtil.buildCsvFileBodyMap(districts);

        //生成
        MyCsvFileUtil.writeFile(fileName, contentBody);

    }
}
